<?php
/**
 * @name 直播系统管理控制器
 * @namespace app\admin\controller;
 * @use app\common\controller\AdminBase;
 * @date 2016-9-19
 * @author zhibo
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
class Msgtext extends AdminBase {

    public function index()
    {
        //发出的站内信
        $sendMsg = M('admin_msg_text')->where(array('send_id'=>decrypt_id(session('aid')),'state'=>1))->order('postdate desc')->findPage(8);
        foreach ($sendMsg['data'] as $k=>$v)
        {
            $groupTitle = M('auth_group')->where(array('id'=>$v['group_id']))->field('title')->find();
            $sendMsg['data'][$k]['msgto'] = $groupTitle['title'];
        }
        $this->assign('sendmsg',$sendMsg['data']);
        $this->assign('sendpage',$sendMsg);
        return $this->fetch();
    }
	
	public function recmessage()
	{
		//收到的站内信
		$recMsg = M('admin_message')->where(array('rec_id'=>decrypt_id(session('aid'))))->order('create_time desc')->findPage(15);
		foreach ($recMsg['data'] as $key=>$val)
		{
			$recMsg['data'][$key]['msgText'] = M('admin_msg_text')->where(array('id'=>$val['msg_id']))->find();
		}
		$this->assign('recmsg',$recMsg['data']);
		$this->assign('recpage',$recMsg);
		return $this->fetch();
    }
    /**
     * @name 发送站内信
     */
    public function addMessage()
    {
        if(IS_POST)
        {
            $data = [
                'group_id'=>intval($this->gorup),
                'msg'=>string_filter($this->msg),
                'title'=>string_filter($this->title),
                'send_id'=>decrypt_id(session('aid')),
                'creater'=>session('username'),
                'type'=>2,
                'state'=>1,
                'postdate'=>time(),
            ];
            if(!$data['group_id'])
            {
                return ['status'=>0,'message'=>'添加失败，没有选择用户组'];
            }
            $titleLen = mb_strlen($data['title'],'UTF8');
            if($titleLen < 1 || $titleLen > 20)
            {
                return ['status'=>0,'message'=>'标题的长度在1-20之间'];
            }
            $msgLen = mb_strlen($data['msg'],'UTF8');
            if($msgLen < 1 || $msgLen > 200)
            {
                return ['status'=>0,'message'=>'站内信的长度在1-200之间'];
            }
            $insertID = M('admin_msg_text')->add($data);
            if($insertID)
            {
                $gourps = M('auth_group_access')->where(array('group_id'=>$data['group_id']))->select();
                foreach ($gourps as $k=>$v)
                {
                    $insertArr = [
                        'rec_id'=>$v['uid'],
                        'msg_id'=>$insertID,
                        'state'=>1,
                        'create_time'=>time()
                    ];
                    M('admin_message')->add($insertArr);
                }
                // 记录操作日志
                $content = '发送站内信';
                $type = 'admin_user';
                $ext = array('key'=>'admin_Msgtext_addMessage');
                system_log($content,$type,$ext);
                return ['status' => 1, 'data' => ['info'=>'发送成功','jumpurl' => U('admin/Msgtext/index')]];
            }else
            {
                return ['status'=>0,'message'=>'发送失败'];
            }
        }
        elseif(IS_GET)
        {
            $gorup = M('auth_group')->where('status = 1')->order('id')->select();
            $this->assign('module',$gorup);
            return $this->fetch();
        }
    }
    /**
     * @name 查看站内信
     */
    public function readMsg()
    {
        if(IS_GET)
        {
            $msgID = decrypt_id($this->id);
            if(!$msgID)
            {
                return ['status'=>0,'message'=>'该消息不存在'];
            }
            //更改消息阅读状态
            M('admin_message')->where(array('rec_id'=>decrypt_id(session('aid')),'msg_id'=>$msgID))->save(array('state'=>2));
            $recMsg = M('admin_msg_text')->where(array('id'=>$msgID))->find();
            $this->assign('msg',$recMsg);
            return $this->fetch();
        }
    }
    /**
     * @name 删除收到的站内信
     */
    public function delMsg()
    {
        if(IS_POST)
        {
            $id = decrypt_id($this->id);
            if(M('admin_message')->where(array('id'=>$id))->delete())
            {
                // 记录操作日志
                $content = '删除收到的站内信';
                $type = 'admin_user';
                $ext = array('key'=>'admin_Msgtext_delMsg');
                system_log($content,$type,$ext);
                return ['status' => 1, 'data' => ['info'=>'删除成功','jumpurl' => U('admin/Msgtext/index')]];
            }else
            {
                return ['status'=>0,'message'=>'该消息不存在'];
            }
        }
    }
    /**
     * @name 删除发送的站内信
     */
    public function delsendMsg()
    {
        if(IS_POST)
        {
            $id = decrypt_id($this->id);
            if($id)
            {
                if(M('admin_msg_text')->where(array('id'=>$id))->save(array('state'=>2)))
                {
                    // 记录操作日志
                    $content = '删除发出的站内信';
                    $type = 'admin_user';
                    $ext = array('key'=>'admin_Msgtext_delsendMsg');
                    system_log($content,$type,$ext);
                    return ['status' => 1, 'data' => ['info'=>'删除成功','jumpurl' => U('admin/Msgtext/index')]];
                }else
                {
                    return ['status'=>0,'message'=>'删除失败'];
                }
            }else
            {
                return ['status'=>0,'message'=>'该消息不存在'];
            }
        }
    }
    /**
     * @name 站内信未读条数
     */
    public function msgNum()
    {
        $msgNum = M('admin_message')->where(array('rec_id'=>decrypt_id(session('aid')),'state'=>1))->count();
//        $msgNum = M('admin_message')->where(array('rec_id'=>10,'state'=>1))->count();
        return ['status' => 1, 'data' => ['count'=>$msgNum]];
    }
}